$(document).ready(function (){
require(['accordion','tree'],function (accordion ,tree){

var left_nav_tree_url = "assetInterfaceManage/queryCntTreeList";

var left_type = new HashMap();
left_type.put("3" ,"securityDomainId");
left_type.put("1" ,"bussinessDomainId");
left_type.put("4" ,"assetTypeId");

var el_accordion = $("#accordion");

var list_current_node;

var list_current_type;

var list_current_data_name;

view_init();

event_init()

$("#accordion_icon").find("div").eq(0).click();

function view_init()
{

	left_tree_init();

	g_formel.interval_refresh_render($("#refresh_btn") ,{
		elTable : $("#table_div1"),
		hideOption : true
	});

	index_list_search_render($("um-search") ,{
		cbf : function (val){
			g_grid.refresh($("#table_div1") ,{
				paramObj : {assetName : val}
			})
		},
		placeholder : "请输入资产名称"
	})
}

function event_init(){
	$("#snmp_config_btn").click(function (){
		snmp_edit_template_init()
	})
}

/** 左侧树初始化*/
function left_tree_init()
{
	um_ajax_post({
		url : left_nav_tree_url,
		paramObj : {cntTree : 1 ,treeType : "ASSET_TYPE_TREE"},
		successCallBack : function (data){
			data = data.assetTypeTreeWithCnt
			data = data.reverse()
			data = _.filter(data ,function (obj){
				return (obj.parentID == "roota" || obj.parentID == "root")
			})
			tree.simpleTree($("#accordion_1") ,{
				zNodes : data,
				zTreeOnClick : function (treeNode){
					list_current_node = treeNode
			 		asset_interface_list(list_current_node.id)
				},
				initIdVal : "roota"
			})
		}
	});
}


function asset_interface_list(nodeId)
{
	g_grid.render($("#table_div1"),{
		 header:[
		 		  {text:'',name:"t",width:3,hideSearch:"hide"},
				  {text:'资产名称',name:"ed_name",width:22,align:"left",searchRender:function (el){
						el.append('<input type="text" search-data="assetName" class="form-control input-sm">');
						el.append('<input type="hidden" search-data="type" value="3" searchCache/>');
				  		el.append('<input type="hidden" search-data="securityDomainId" searchCache/>');
				  		el.append('<input type="hidden" search-data="bussinessDomainId" searchCache/>');
				  		el.append('<input type="hidden" search-data="assetTypeId" searchCache/>');
				  }},
				  {text:'主IP',name:"ip",width:22,align:"left" ,searchRender:function (el){
						index_render_div(el ,{type:"ip"});
				  }},
				  {text:'管控域',name:"securityDomainNames",width:16,align:"left",hideSearch:true,searchRender:function (el){
				  }},
				  {text:'资产类型',name:"equipType",width:16,align:"left",hideSearch:true,searchRender:function (el){
				  }},
				  {text:'接口数量',name:"portSumNum",width:9,align:"left",hideSearch:true},
				  {text: 'SNMP',name: "ed_id",width:11,align:"left",hideSearch:true,render:function (text,rowData){
				  	if(rowData.snmpStatus === '0'){
				  		return '未配置'
				  	}else {
				  		return '<a data-type="aclick" href="javascript:void(0);" style="margin:0 3px">已配置</a>';
				  	}
				  },click:function(rowData){
				  	if(rowData.snmpStatus === '0'){
				  		return false;
				  	}
				  	um_ajax_get({
				  		server: index_web_app_name,
				  		url : 'module/base_resource_mant/snmp_manage_tpl.html',
				  		successCallBack: function (data){
				  			g_dialog.dialog($(data).find('#snmp_manage_detail_template'),{
				  				title : "SNMP详情",
				  				width : "830px",
				  				autoHeight : true,
				  				initAfter : function (el){
				  					el.find("[data-id=codename]").hide().prev().hide()
				  					el.find('[data-id=snmpVersion]').prev().removeClass('col-lg-2').addClass('col-lg-3');
				  					um_ajax_get({
				  						url : 'AssetOperation/queryAssetTelnetAndSSHAndSnmp',
				  						paramObj : {edIds : rowData.ed_id},
				  						successCallBack : function (data1){
				  							console.log(data1)
				  							var snmpData = data1[0].snmp;
				  							var authWay_map = new HashMap();
				  							authWay_map.put("" ,"");
				  							authWay_map.put("1" ,"SHA");
				  							authWay_map.put("2" ,"MD5");

				  							var encryptionWay_map = new HashMap();
				  							encryptionWay_map.put("" ,"");
				  							encryptionWay_map.put("1" ,"3DES");
				  							encryptionWay_map.put("2" ,"AES128");
				  							encryptionWay_map.put("3" ,"AES192");
				  							encryptionWay_map.put("4" ,"AES256");
				  							encryptionWay_map.put("5" ,"DES");

				  							var version_map = new HashMap();
				  							version_map.put("1" ,"SNMP V1");
				  							version_map.put("2" ,"SNMP V2c");
				  							version_map.put("3" ,"SNMP V3");

				  							el.find('#query_form').umDataBind("render",snmpData)

				  							el.find("[data-id=authWay]").text(authWay_map.get(snmpData.authWay));
				  							el.find("[data-id=encryptionWay]").text(encryptionWay_map.get(snmpData.encryptionWay));
				  							el.find("[data-id=snmpVersion]").text(version_map.get(snmpData.snmpVersion));
				  							if(snmpData.authPwd && snmpData.authPwd.length != 0)
				  							{
				  								el.find("[data-id=authPwd]").text(snmpData.authPwd.replace(/.*/g ,"*"));
				  							}
				  							else
				  							{
				  								return;
				  							}

				  							if(snmpData.encryptionPwd && snmpData.encryptionPwd.length != 0)
				  							{
				  								el.find("[data-id=encryptionPwd]").text(snmpData.encryptionPwd.replace(/.*/g ,"*"));
				  							}
				  							else
				  							{
				  								return;
				  							}
				  						}
				  					})
				  				}
				  			})
				  		}
				  	})
				  }}
				 ],
		 oper:[
				  {icon:"rh-icon rh-edit" ,text:"修改" ,aclick:asset_interface_list_update_dialog}
			   ],
		 operWidth:"60px",
		 url:"assetInterfaceManage/queryAssetListNew",
		 paramObj : {type:"4",assetTypeId:nodeId},
		 hasBorder : false,
		 autoHeight : true,
		 showCount : true,
		 dbIndex:1,
		 dbClick : asset_interface_detail,
		 allowCheckBox : true,
		 tdClick: true
	});
}

function asset_interface_detail(rowData)
{		
	$.ajax({
		type: "GET",
		url: "module/monitor_obj/asset_interface_tpl.html",
		success :function(data)
		{
			g_dialog.dialog($(data).find("[id=asset_interface_template_detail]"),{
				width:"700px",
				init:init,
				initAfter:initAfter,
				isDetail:true,
				title:"资产接口查看",
				autoHeight:"autoHeight",
				saveclick:save_click
			});
		}
	});

	function init(el)
	{
	   el.find("[id=asset_interface_template_detail]").umDataBind("render" ,rowData);					
	}

	function initAfter(el)
	{
		var dialog_table_url = "assetInterfaceManage/queryAssetInterface";
		var dialog_table_header = [
			{text:'',name:"t",width:3,hideSearch:"hide"},
			{text:'接口索引',name:"interface_ind",align:"left"},
			{text:'接口名称',name:"interface_name",align:"left"},
			{text:'接口描述',name:"interface_desc",align:"left"},
			{text:'接口类型',name:"interface_type",align:"left"},
			{text:'接口带宽(M)',name:"interface_speed",align:"left",render:function(data){
					return Math.round(data/(1000*1000));
			}}
	   ];
		g_grid.render(el.find("[id=dialog_table]"),{
			url:dialog_table_url,
			header:dialog_table_header, 
	 		paramObj : {ed_id:rowData.ed_id},
			allowCheckBox:false,
			maskObj:el.find("[id=dialog_table]"),
			paginator:false,
			hideSearch:true
		});
	}

	function save_click(el)
	{
		g_dialog.hide(el);
	}
}

function asset_interface_list_update_dialog(rowData)
{
	$.ajax({
		type: "GET",
		url: "module/monitor_obj/asset_interface_tpl.html",
		success :function(data)
		{
			g_dialog.dialog($(data).find("[id=template_update]"),{
				width:"1000px",
				height:"500px",
				init:init,
				title:"资产接口修改",
				isDetail:true,
				autoHeight : "autoHeight",
				autoHeightValue : "10px",
				closeCbf : function (){
					g_grid.refresh($("#table_div1"));
				}
			});
		}
	});

	function init(el)
	{
		var template_update = $("[data-id=template_update_div]");
		var paramTmp = new Object();
		paramTmp.ed_id = rowData.ed_id;
		template_update.umDataBind("render" ,rowData);

		interface_list();

		template_update.find("[data-id=template_update_add_btn]").click(function (){
			interface_add_update_dialog();
		});

		template_update.find("[data-id=template_datch_delete_btn]").click(function (){
			index_update_batch_delete(rowData);
		});

		template_update.find('[data-id=template_batch_show]').click(function (){
			index_update_batch_show(rowData);
		});

		template_update.find('[data-id=template_batch_hide]').click(function (){
			index_update_batch_hide(rowData);
		});

		template_update.find("[data-id=template_scan_btn]").click(function (){
			g_dialog.operateConfirm("本次扫描会覆盖上一次扫描结果,需要对资产进行接口扫描吗？" ,{
				saveclick : function()
				{
					$.ajax({
						type: "GET",
						url: "module/monitor_obj/asset_interface_tpl.html",
						success :function(data)
						{
							g_dialog.dialog($(data).find("[id=asset_interface_scan_template_detail]"),{
								width:"600px",
								title:"接口扫描",
								init:init,
								initAfter:initAfter,
								saveclick:save_click,
								isDetail : true,
								closeCbf : function (){
									interface_list({maskObj : $("#tablegrid1")});
								}
							});
						}
					});

					function init(el)
					{
						el.umDataBind("render" ,rowData);
						
						el.find("[data-id=result]").text("扫描中...");
					}

					function initAfter(el)
					{	
						um_ajax_post({
							url : "assetInterfaceManage/scanAsset",
							paramObj : {"ed_id":rowData.ed_id},
							maskObj : el,
							successCallBack : function(rData){
								rowData.result = rData.result;
								el.find("[id=asset_interface_scan_template_detail]").umDataBind("render" ,rowData);
							},
							failCallBack : function (){
								el.find("[data-id=result]").text("扫描失败");
							}
						});
					}

					function save_click(el)
					{
						interface_list({maskObj : $("#tablegrid1")});
						g_dialog.operateAlert(null ,"操作成功！");
						g_dialog.hide(el);
					}							
				}
			});				
			// 	maskObj : template_update.parent()
		});


		function interface_list()
		{
			g_grid.render(template_update.find("[data-id=tablegrid1]"),{
				header:[
						  {text:'接口索引',name:"interface_ind",align:"left",width:16},
						  {text:'接口名称',name:"interface_name",align:"left",width:22},
						  {text:'接口描述',name:"interface_desc",align:"left",width:24},
						  {text:'接口类型',name:"interface_type",align:"left",width:14},
						  {text:'接口带宽(M)',name:"interface_speed",align:"left",width:13,render:function(data){
						  		return Math.round(data/(1000*1000));
						  }},
						  {text:'显示',name:'is_visuable',align:"left",width:11,render:function (data){
						  	
						  	return data=='1'?'不显示':'显示';
						  }}
						 ],
				oper:[
						  {icon:"rh-icon rh-edit" ,text:"修改" ,aclick:interface_update_dialog},
						  {icon:"rh-icon rh-delete" ,text:"删除" ,aclick:interface_delete_dialog}
					   ],
				operWidth:"80px",
				paginator : false,
				url : "assetInterfaceManage/queryAssetInterface",
				paramObj : {ed_id:rowData.ed_id},
				maskObj:$("[data-id=tablegrid1]"),
				hideSearch:true,
				cacheSearch : true,
				searchInp : $("[id=table_flow_search_inp]"),
				searchKey : ['interface_ind','interface_name','interface_desc','interface_type']
			});
		}

		function interface_add_update_dialog(rData)
		{
			var title = rData ? "接口修改" : "接口新增";
			var add_update_url = "assetInterfaceManage/addPort";
			if (rData)
			{
				add_update_url = "assetInterfaceManage/updPort";
			}
			$.ajax({
				type: "GET",
				url: "module/monitor_obj/asset_interface_tpl.html",
				success :function(data)
				{
					g_dialog.dialog($(data).find("[id=template_add]"),{
						width:"500px",
						height:"450px",
						title :title,
						init:init,
						saveclick:save_click
					});
				}
			});
			function init()
			{
				var template_add_div = $("[data-id=template_add_div]");
				g_validate.init(template_add_div.find("form"));
				if (rData)
				{
					template_add_div.umDataBind("render" ,rData);
					template_add_div.find("[data-id=interface_speed]").val(parseInt(rData.interface_speed)/(1000*1000));
				}
			}
			function save_click()
			{
				var template_add_div = $("[data-id=template_add_div]");
				if (g_validate.validate(template_add_div))
				{
					var portStore = template_add_div.umDataBind("serialize");
					portStore.interface_speed = (portStore.interface_speed * 1000 * 1000);
					var oldStore ;
					if (rData)
					{
						oldStore = rData;
						oldStore.ed_id = rowData.ed_id;
					}
					var equipStore = rowData;
					var obj = {};
					obj.portStore = portStore;
					obj.equipStore = equipStore;
					obj.oldStore = oldStore;
				
					um_ajax_post({
						url : add_update_url,
						paramObj : obj,
						successCallBack : function (){
							g_dialog.operateAlert(template_update.parent() ,"编辑成功!");
							interface_list();
						}
					});
					return true;
				}
			}
		}

		function interface_update_dialog(rData)
		{
			interface_add_update_dialog(rData);
		}

		function interface_delete_dialog(rData)
		{
			g_dialog.operateConfirm("确认进行删除操作么?",{
				saveclick:function (){
					var interfaceStore = rData;
					var equipStore = rowData;
					var obj = {};
					obj.interfaceStore = interfaceStore;
					obj.equipStore = equipStore;
					um_ajax_post({
						url : "assetInterfaceManage/delPort",
						paramObj : obj,
						successCallBack : function (){
							g_dialog.operateAlert(template_update.parent() ,"删除成功!");
							interface_list();
							//g_grid.refresh($("#table_div1"));
						}
					});
				}
			});
		}
	}

	function index_update_batch_delete(rowData)
	{
		var dataArray = g_grid.getData($("[data-id=tablegrid1]") ,{chk : true});

		if(dataArray.length === 0){
			g_dialog.operateAlert($("[data-id=tablegrid1]") ,index_select_one_at_least_msg ,"error");
			return false;
		}

		// var data = g_grid.getData($("[data-id=tablegrid1]") ,{chk:true});

		// var tmp=[];
		var interfaceStore = dataArray;
		var equipStore = [];
		for (var i = 0; i < dataArray.length; i++) {
			equipStore.push(rowData);
		}

		g_dialog.operateConfirm("确认删除选中的记录么" ,{
			saveclick : function()
			{
				um_ajax_post({
					url : "assetInterfaceManage/batchDelPort",
					paramObj : {interfaceStore:dataArray ,equipStore:equipStore},
					successCallBack : function(data){
						g_grid.refresh($("[data-id=tablegrid1]") ,{
							queryBefore : function (paramObj){
								paramObj.ed_id = rowData.ed_id;
							}
						});
						//g_grid.refresh($("#table_div1"));
						g_dialog.operateAlert(null ,"操作成功！");
					}
				});
			}
		});
	}

	function index_update_batch_show(rowData){
		var dataArray = g_grid.getData($("[data-id=tablegrid1]") ,{chk : true});

		if(dataArray.length === 0){
			g_dialog.operateAlert($("[data-id=tablegrid1]") ,index_select_one_at_least_msg ,"error");
			return false;
		}

		// var data = g_grid.getData($("[data-id=tablegrid1]") ,{chk:true});

		// var tmp=[];
		var interfaceStore = dataArray;
		var equipStore = [];
		for (var i = 0; i < dataArray.length; i++) {
			equipStore.push(rowData);
		}

		um_ajax_post({
			url : "assetInterfaceManage/batchSetInterfaceVisuable",
			paramObj : {interfaceStore:dataArray ,equipStore:equipStore},
			successCallBack : function(data){
				g_grid.refresh($("[data-id=tablegrid1]") ,{
					queryBefore : function (paramObj){
						paramObj.ed_id = rowData.ed_id;
					}
				});
				//g_grid.refresh($("#table_div1"));
				g_dialog.operateAlert(null ,"操作成功！");
			}
		});
	}
	function index_update_batch_hide(rowData){
		var dataArray = g_grid.getData($("[data-id=tablegrid1]") ,{chk : true});

		if(dataArray.length === 0){
			g_dialog.operateAlert($("[data-id=tablegrid1]") ,index_select_one_at_least_msg ,"error");
			return false;
		}

		// var data = g_grid.getData($("[data-id=tablegrid1]") ,{chk:true});

		// var tmp=[];
		var interfaceStore = dataArray;
		var equipStore = [];
		for (var i = 0; i < dataArray.length; i++) {
			equipStore.push(rowData);
		}

		um_ajax_post({
			url : "assetInterfaceManage/batchSetInterfaceUnVisuable",
			paramObj : {interfaceStore:dataArray ,equipStore:equipStore},
			successCallBack : function(data){
				g_grid.refresh($("[data-id=tablegrid1]") ,{
					queryBefore : function (paramObj){
						paramObj.ed_id = rowData.ed_id;
					}
				});
				//g_grid.refresh($("#table_div1"));
				g_dialog.operateAlert(null ,"操作成功！");
			}
		});
	}
}

function snmp_edit_template_init()
{
	var title = "SNMP配置";
	$.ajax({
		type: "GET",
		url: "module/base_resource_mant/snmp_manage_tpl.html",
		success :function(data)
		{
			g_dialog.dialog($(data).find("[id=snmp_manage_edit_template]"),{
				width:"860px",
				title:title,
				init:init,
				saveclick:save_click
			});
		}
	});

	function init(el)
	{

		el.find('#add_snmp_name_div').remove();
		el.find('[data-id=snmpVersion]').parent().prev().removeClass('col-lg-2').addClass('col-lg-3');
		el.find("[data-id=snmpVersion]").change(function(){
			var tmp = $(this).val();
			if (tmp == "1" || tmp == "2")
			{
				el.find("[data-id=snmpUserName]").attr("disabled", "disabled");//用户名
				el.find("[data-id=snmpUserName]").val("");
				el.find("[data-id=authWay]").val("");
				el.find("[data-id=authWay]").attr("disabled", "disabled");//认证方式
				el.find("[data-id=authPwd]").attr("disabled", "disabled");//认证口令
				el.find("[data-id=encryptionWay]").attr("disabled", "disabled");//加密方式
				el.find("[data-id=encryptionPwd]").attr("disabled", "disabled");//加密口令
				el.find("[data-id=context]").attr("disabled", "disabled");//Context ID
				el.find("[data-id=context]").val("");
				el.find("[data-id=contextName]").attr("disabled", "disabled");//Context Name
				el.find("[data-id=contextName]").val("");
				el.find("[data-id=snmpCommunity]").removeAttr("disabled", "disabled");//读COMMUNITY
				el.find("[data-id=snmpWCommunity]").removeAttr("disabled", "disabled");//写COMMUNITY

				g_validate.clear([el.find("[data-id=snmpName]") ,el.find("[data-id=snmpPort]") ,
					el.find("[data-id=timeOut]") ,el.find("[data-id=tryTimes]") ,
					el.find("[data-id=context]") ,el.find("[data-id=contextName]") ,
					el.find("[data-id=snmpUserName]") ,el.find("[data-id=authWay]") ,
					el.find("[data-id=snmpCommunity]") ,el.find("[data-id=snmpWCommunity]") ,
					el.find("[data-id=authPwd]") ,el.find("[data-id=encryptionPwd]")]);

				el.find("[data-id=authWay]").val("");
				el.find("[data-id=encryptionWay]").val("");
			}
			
			else if (tmp == "3")
			{
				el.find("[data-id=snmpCommunity]").attr("disabled", "disabled");//读COMMUNITY
				el.find("[data-id=snmpWCommunity]").attr("disabled", "disabled");//写COMMUNITY
				el.find("[data-id=authPwd]").attr("disabled", "disabled");
				el.find("[data-id=encryptionWay]").attr("disabled", "disabled");
				el.find("[data-id=encryptionPwd]").attr("disabled", "disabled");

				el.find("[data-id=snmpUserName]").removeAttr("disabled");//用户名
				el.find("[data-id=authWay]").removeAttr("disabled");//认证方式
				el.find("[data-id=context]").removeAttr("disabled");//Context ID
				el.find("[data-id=contextName]").removeAttr("disabled");//Context Name

				
				g_validate.clear([el.find("[data-id=snmpName]") ,el.find("[data-id=snmpPort]") ,
					el.find("[data-id=timeOut]") ,el.find("[data-id=tryTimes]") ,
					el.find("[data-id=snmpCommunity]") ,el.find("[data-id=snmpWCommunity]")]);
				el.find("[data-id=snmpCommunity]").val("");//读COMMUNITY
				el.find("[data-id=snmpWCommunity]").val("");//写COMMUNITY
			}	

			el.find("select").not("[data-id=snmpVersion]").trigger("change");
		});

		el.find("[data-id=authWay]").change(function(){
			var tmp = $(this).val();
			if (tmp == "")
			{
				el.find("[data-id=authPwd]").attr("disabled", "disabled");//认证口令
				el.find("[data-id=encryptionWay]").attr("disabled", "disabled");//加密方式
				el.find("[data-id=encryptionPwd]").attr("disabled", "disabled");//加密口令

				el.find("[data-id=authPwd]").val("");//认证口令
				el.find("[data-id=encryptionWay]").val("");//加密方式
				el.find("[data-id=encryptionPwd]").val("");//加密口令
			}

			else
			{
				el.find("[data-id=authPwd]").removeAttr("disabled");//认证口令
				el.find("[data-id=encryptionWay]").removeAttr("disabled");//加密方式
			}
			el.find("[data-id=encryptionWay]").trigger("change");
		});

		el.find("[data-id=encryptionWay]").change(function(){
			var tmp = $(this).val();
			if (tmp == "")
			{
				el.find("[data-id=encryptionPwd]").attr("disabled", "disabled");//加密口令
				el.find("[data-id=encryptionPwd]").val("");
			}

			else
			{
				el.find("[data-id=encryptionPwd]").removeAttr("disabled");//加密口令													
			}
		});

		el.find("[id=update_snmp_name_div]").remove();		
    }

	function save_click(el ,saveObj)
	{
		if (!g_validate.validate($("#snmp_manage_edit_template")))
		{
			return false;
		}

		else
		{
			var data = g_grid.getData($('#table_div1'),{chk : true});

			if(data.length == 0){
				g_dialog.operateAlert(el,"请选择资产",'error');
				return false;
			}

			var saveList = [];

			for(var i=0;i<data.length;i++){
				var obj = $.extend(true,saveObj,{});
				 obj.edId = data[i].ed_id;
				 saveList.push(obj)
			}

			um_ajax_post({
				url : "assetInterfaceManage/batchSetAssetSnmp",
				paramObj : {snmpVos : saveList},
				maskObj : el,
				successCallBack : function (data){
					g_dialog.operateAlert(null ,title+"成功！");
					g_dialog.hide(el);
					g_grid.refresh($("#table_div1"))
				}
			});
		}
	}
}


});
});